package com.nameli.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameli.SIM.bean.dto.CheckMajorDTO;
import com.nameli.SIM.bean.entity.CheckMajorInfo;
import com.nameli.SIM.bean.entity.MajorInfo;
import com.nameli.SIM.bean.vo.CheckMajorVO;
import com.nameli.SIM.mapper.CheckMajorInfoMapper;
import com.nameli.SIM.mapper.MajorInfoMapper;
import com.nameli.SIM.util.RedisUtil;
import com.nameli.admin.service.CheckMajorInfoService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @author NAME-L
 * @Description TODO
 * @className CheckMajorRecordServiceImpl
 * @date 2021-11-23 10:20:58
 */
@Component
@Transactional(rollbackFor = {Exception.class}, timeout = 3)
public class CheckMajorInfoServiceImpl implements CheckMajorInfoService {

    private static final Log log = LogFactory.getLog(CheckMajorInfoServiceImpl.class);

    @Autowired
    private CheckMajorInfoMapper checkMajorInfoMapper;
    @Autowired
    private MajorInfoMapper majorInfoMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public PageInfo<CheckMajorVO> queryAllCheckMajor(Integer pageIndex, Integer pageSize) throws Exception {
        log.info("enter into -----------> CheckMajorInfoServiceImpl - queryAllCheckMajor");
        log.info("pageIndex:" + pageIndex);
        log.info("pageSize:" + pageSize);
        PageHelper.startPage(pageIndex, pageSize);
        List<CheckMajorVO> checkMajorVOS = checkMajorInfoMapper.queryAllCheckMajor();
        log.info("output -----------> checkMajorVOS:" + checkMajorVOS);
        PageInfo<CheckMajorVO> checkMajorVOPageInfo = new PageInfo<>(checkMajorVOS);
        return checkMajorVOPageInfo;
    }

    @Override
    public Boolean updateCheckMajor(CheckMajorDTO checkMajorDTO) {
        log.info("enter into -----------> CheckMajorInfoServiceImpl - updateCheckMajor");
        log.info("确认审核的记录checkMajorDto:" + checkMajorDTO);
        String checkType = checkMajorDTO.getCheckType();//审核类型
        Integer methodType = checkMajorDTO.getMethodType();//操作类型
        log.info("审核类型为checkType:" + checkType);
        checkMajorDTO.setCheckStatus(3);//3 - 审核通过
        String key = null;
        if (methodType.equals(1)) {
            key = "insert_" + checkMajorDTO.getMajorNo();
        }else {
            key = "update_" + checkMajorDTO.getMajorNo();
        }
        Boolean checkflag = false;
        Boolean flag = false;
        log.info("以 “" + key + "”为键，从redis中查值");
        if (redisUtil.hasKey(key)) {
            flag = majorInfoMapper.updateByCheck(checkMajorDTO);
            checkflag = checkMajorInfoMapper.updateCheckMajor(checkMajorDTO);
            log.info("专业信息表状态修改 - flag:" + flag);
            log.info("专业审核表状态修改 - checkflag:" + checkflag);
            if (flag && checkflag) {
                log.info("redis存在key-value，删除key");
                redisUtil.del(key);
                return true;
            }
        } else {
            log.info("redis不存在key-value");
            return false;
        }
        return false;
    }


    @Override
    public Boolean updateRejectMajor(CheckMajorDTO checkMajorDTO) {
        log.info("enter into -----------> CheckMajorInfoServiceImpl - updateRejectMajor");
        log.info("驳回审核的记录checkMajorDto:" + checkMajorDTO);
        String checkType = checkMajorDTO.getCheckType();//
        Integer methodType = checkMajorDTO.getMethodType();
        log.info("审核类型为checkType:" + checkType);
        checkMajorDTO.setCheckStatus(4);
        String key = null;
        if (methodType.equals(1)) {
            key = "insert_" + checkMajorDTO.getMajorNo();
        }else {
            key = "update_" + checkMajorDTO.getMajorNo();
        }
        log.info("以 “" + key + "”为键，从redis中取值");
        Boolean flag = false;
        Boolean checkflag = false;
        if (redisUtil.hasKey(key)) {
            log.info("redis存在被驳回的专业信息原数据");
            MajorInfo majorInfo = (MajorInfo) redisUtil.get(key);
            log.info("专业信息原数据 ： " + majorInfo);
            if (methodType.equals(1)) {
                log.info("驳回新增专业信息，删除该专业");
                majorInfo.setStatus(5);
            }
            majorInfo.setCheckStatus(4);
            flag = majorInfoMapper.updateByReject(majorInfo);
            checkflag = checkMajorInfoMapper.updateCheckMajor(checkMajorDTO);
            if (flag && checkflag) {
                log.info("从redis中删除 key：" + key);
                redisUtil.del(key);
            }
            return true;
        } else {
            log.info("redis中不存在该值，无法驳回");
        }
        return false;
    }
}
